package com.dicsystems.myrec;

import java.util.List;

import com.dicsystems.myrec.dto.Category;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

public class CategoryDialog extends Dialog {

	private Context me;
	private List<Category> categories;
	private Button category;
	private ImageButton clear_category;
	private ListView list;

	protected CategoryDialog(Context context, List<Category> categories, Button category) {
		super(context);
		this.me = context;
		this.categories = categories;
		this.category = category;
	}

	protected CategoryDialog(Context context, List<Category> categories, Button category, ImageButton clear_category) {
		super(context);
		this.me = context;
		this.categories = categories;
		this.category = category;
		this.clear_category = clear_category;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.category_list);
		list = (ListView)findViewById(android.R.id.list);
		list.setAdapter(new CategoryAdapter(me, categories));
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				category.setTag(categories.get(position).id);
				category.setText(categories.get(position).name);
				if(clear_category != null) {
					clear_category.setVisibility(View.VISIBLE);
				}
				dismiss();
			}
		});
	}
}
